package com.cjj.elasticsearch.entity;

import com.cjj.swagger.config.ResConst;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;

import java.util.List;

/**
 * @author DoyuTu
 * @version 0.0.1 java-asset-data-sync
 */
@Data
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@ApiModel("分页报文")
public class ResPageContent<T> {

    @ApiModelProperty(value = "返回码,200为正常,详情参考http状态码", example = "200")
    private int code;
    @ApiModelProperty(value = "返回描述信息", example = "成功")
    private String message;
    @ApiModelProperty("返回数据")
    private List<T> data;
    private int page = 1;
    private int size = 20;
    @ApiModelProperty("有下一页")
    private boolean hasNext = false;
    @ApiModelProperty("总页数")
    private int pages = 1;
    @ApiModelProperty("总数")
    private long total = 0;

    private ResPageContent(int code, String message) {
        this.code = code;
        this.message = message;
    }

    private static <T> ResPageContent<T> of(int code, String message) {
        return new ResPageContent<>(code, message);
    }

    private static <T> ResPageContent<T> of(int code, String message, List<T> data, PageInfo info) {
        return new ResPageContent<>(code, message, data, info.getPageNum(), info.getPageSize(), info.isHasNextPage(), info.getPages(), info.getTotal());
    }

    private static <T> ResPageContent<T> of(int code, String message, List<T> data, AggregatedPage info) {
        long total = NumberUtils.min(info.getTotalElements(), 10000);
        int size = info.getSize();
        return new ResPageContent<>(code, message, data, info.getNumber() + 1, size, info.hasNext(), (int) (total + size - 1) / size, total);
    }

    public static <T> ResPageContent<T> ok() {
        return of(ResConst.OK.getCode(), ResConst.OK.getMessage());
    }

    public static <T> ResPageContent<T> ok(List<T> data, PageInfo info) {
        return of(ResConst.OK.getCode(), ResConst.OK.getMessage(), data, info);
    }

    public static <T> ResPageContent<T> ok(PageInfo<T> info) {
        return ok(info.getList(), info);
    }

    public static <T> ResPageContent<T> ok(AggregatedPage<T> info) {
        return ok(info.getContent(), info);
    }

    public static <T> ResPageContent<T> ok(List<T> data, AggregatedPage info) {
        return of(ResConst.OK.getCode(), ResConst.OK.getMessage(), data, info);
    }

    public static <T> ResPageContent<T> error(ResConst resp) {
        return of(resp.getCode(), resp.getMessage());
    }

    public static <T> ResPageContent<T> error(String message) {
        return of(ResConst.BUSINESS_ERROR.getCode(), message);
    }

    public static <T> ResPageContent<T> error(ResConst resp, String message) {
        return of(resp.getCode(), message);
    }

}
